package com.xinli.business.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.TQuestionFollow;
import com.xinli.business.domain.TZxsFollow;
import com.xinli.business.mapper.TZxsFollowMapper;
import com.xinli.business.service.ITZxsFollowService;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 咨询师关注Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class TZxsFollowServiceImpl extends ServiceImpl<TZxsFollowMapper, TZxsFollow> implements ITZxsFollowService {
    @Autowired
    private TZxsFollowMapper tZxsFollowMapper;

    /**
     * 查询咨询师关注
     *
     * @param id 咨询师关注主键
     * @return 咨询师关注
     */
    @Override
    public TZxsFollow selectTZxsFollowById(String id) {
        return tZxsFollowMapper.selectTZxsFollowById(id);
    }

    /**
     * 查询咨询师关注列表
     *
     * @param tZxsFollow 咨询师关注
     * @return 咨询师关注
     */
    @Override
    public List<TZxsFollow> selectTZxsFollowList(TZxsFollow tZxsFollow) {
        return tZxsFollowMapper.selectTZxsFollowList(tZxsFollow);
    }

    /**
     * 新增咨询师关注
     *
     * @param tZxsFollow 咨询师关注
     * @return 结果
     */
    @Override
    public int insertTZxsFollow(TZxsFollow tZxsFollow) {
        tZxsFollow.setCreateTime(DateUtils.getNowDate());
        return tZxsFollowMapper.insertTZxsFollow(tZxsFollow);
    }

    /**
     * 修改咨询师关注
     *
     * @param tZxsFollow 咨询师关注
     * @return 结果
     */
    @Override
    public int updateTZxsFollow(TZxsFollow tZxsFollow) {
        tZxsFollow.setUpdateTime(DateUtils.getNowDate());
        return tZxsFollowMapper.updateTZxsFollow(tZxsFollow);
    }

    /**
     * 批量删除咨询师关注
     *
     * @param ids 需要删除的咨询师关注主键
     * @return 结果
     */
    @Override
    public int deleteTZxsFollowByIds(String[] ids) {
        return tZxsFollowMapper.deleteTZxsFollowByIds(ids);
    }

    /**
     * 删除咨询师关注信息
     *
     * @param id 咨询师关注主键
     * @return 结果
     */
    @Override
    public int deleteTZxsFollowById(String id) {
        return tZxsFollowMapper.deleteTZxsFollowById(id);
    }


    @Override
    public Boolean followZxs(String businessId) {
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<TZxsFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId)
                .eq("user_id", userId);
        List<TZxsFollow> existingFollow = this.list(queryWrapper);

        if (CollectionUtil.isEmpty(existingFollow)) {
            // 未关注，新增关注记录
            TZxsFollow newFollow = new TZxsFollow();
            newFollow.setBusinessId(businessId);
            newFollow.setUserId(userId);
            newFollow.setCreateTime(DateUtils.getNowDate());
            this.save(newFollow);
            return true;
        } else {
            // 已关注，取消关注（删除记录）
            this.remove(queryWrapper);
            return false;
        }


    }

    @Override
    public Boolean isfollowZxs(String businessId) {
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<TZxsFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId)
                .eq("user_id", userId);
        Long count = this.count(queryWrapper);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }





}
